package com.tianqu.tms.www.permission.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.entity.UserAuth;
import com.tianqu.tms.entity.UserRole;

/**
 * @author zhangxueliang 权限删除结果controller
 */
public class PermissionDeleteResultController extends TmsController {
	private Logger logger = Logger
			.getLogger(PermissionDeleteResultController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();

			UserRole userRole = UserRole.getInstance();
			UserAuth userAuth = UserAuth.getInstance();
			int role = (Integer) uc.getAttribute("role");

			// 删除3张表的数据
			userRole.deleteRole(role);
			userAuth.deleteAuth(role);

			logger.debug(this.getClass().getName() + "#execute was completed.");
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

}
